package com.puhui.lib.utils

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Context
import android.graphics.Color
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.TextView
import android.widget.Toast
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.SizeUtils
import com.blankj.utilcode.util.ToastUtils
import com.puhui.lib.R

class ToastUtil {

    companion object {

        private fun init() {
//        ToastUtils.getDefaultMaker().setGravity(Gravity.CENTER, 0, 0)
//        ToastUtils.getDefaultMaker().setTextColor(Color.WHITE)
//        ToastUtils.getDefaultMaker().setBgResource(R.drawable.shape_toast_bg)
//        ToastUtils.getDefaultMaker().setTextSize(15)
            ToastUtils.setGravity(Gravity.CENTER, 0, 0)
            ToastUtils.setMsgColor(Color.WHITE)
            ToastUtils.setBgResource(R.drawable.shape_toast_bg)
            ToastUtils.setMsgTextSize(15)
        }

        /**
         * @see isShort 控制toast显示时长
         */
        @SuppressLint("InflateParams")
        fun showToast(message: String?, isShort: Boolean = true, isCenter: Boolean = true) {
            message?.let {
                val context = ActivityUtils.getTopActivity()
                context?.runOnUiThread {
                    val inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
                    val toastView: View = inflater.inflate(R.layout.toast_view_layout, null)
                    if (toastView is TextView) {
                        toastView.text = message
                        toastView.gravity = Gravity.CENTER

                        val toast = Toast(context)
                        toast.view = toastView
                        if (isCenter) toast.setGravity(Gravity.CENTER, 0, SizeUtils.dp2px(50f))
                        toast.duration = if (isShort) Toast.LENGTH_SHORT else Toast.LENGTH_LONG
                        toast.show()
                    }
                }
            }


//            init()
//            message?.let { ToastUtils.showShort(message) }

//            message?.let { toastMsg ->
//                val activity = ActivityUtils.getTopActivity()
//                activity?.let {
//                    if (!activity.isFinishing) {
//                        addToastView(it, toastMsg, duration)
//                    }
//                }
//            }
        }

        @SuppressLint("InflateParams")
        private fun addToastView(activity: Activity, message: String, duration: Int) {

            val decorView = activity.window?.decorView
            decorView?.let {
                val toastView = LayoutInflater.from(activity).inflate(R.layout.toast_view_layout, null)
                if (toastView is TextView) {
                    toastView.text = message

                    //方式一
//                    val layoutParams = ViewGroup.MarginLayoutParams(
//                        ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT
//                    )
//                    layoutParams.topMargin = ScreenUtils.getScreenHeight() / 2
//                    layoutParams.leftMargin = ScreenUtils.getScreenWidth() / 2 - SizeUtils.getMeasuredWidth(toastView) / 2
//                    activity.window.addContentView(toastView, layoutParams)

                    //方式二
                    if (decorView is FrameLayout) {
                        val layoutParams = FrameLayout.LayoutParams(
                            ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT
                        )

                        layoutParams.topMargin = SizeUtils.dp2px(50f) //title栏的高度
                        layoutParams.gravity = Gravity.CENTER
                        toastView.layoutParams = layoutParams
                        decorView.addView(toastView)

                        decorView.postDelayed({ decorView.removeView(toastView) }, duration.toLong())
                    }
                }
            }
        }
    }
}